This source file includes following definitions.
- __wrap_abort
- __wrap_calloc
- __wrap_getenv
- __wrap_realloc
- __wrap_setenv
- __wrap_unsetenv
- __wrap_getpid
- __wrap_setgrent
- __wrap_getgrent
- __wrap_endgrent
- __wrap_fopen
- __wrap_fopen64
- __wrap_getpwnam_r
- __wrap_readlink
- __wrap_strdup
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <errno.h>
13 #include <pwd.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <setjmp.h>
22 #include <sys/types.h>
23 #include <sys/utsname.h>
24 #include <unistd.h>
25 #include <grp.h>
26
27 #include <cmocka.h>
28 #include <crm/common/unittest_internal.h>
29 #include "mock_private.h"
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 _Noreturn void
71 __wrap_abort(void)
72 {
73 #if (PCMK__WITH_COVERAGE == 1)
74 __gcov_dump();
75 #endif
76 __real_abort();
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90 bool pcmk__mock_calloc = false;
91
92 void *
93 __wrap_calloc(size_t nmemb, size_t size)
94 {
95 if (!pcmk__mock_calloc) {
96 return __real_calloc(nmemb, size);
97 }
98 check_expected(nmemb);
99 check_expected(size);
100 return NULL;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115 bool pcmk__mock_getenv = false;
116
117 char *
118 __wrap_getenv(const char *name)
119 {
120 if (!pcmk__mock_getenv) {
121 return __real_getenv(name);
122 }
123 check_expected_ptr(name);
124 return mock_ptr_type(char *);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 bool pcmk__mock_realloc = false;
140
141 void *
142 __wrap_realloc(void *ptr, size_t size)
143 {
144 if (!pcmk__mock_realloc) {
145 return __real_realloc(ptr, size);
146 }
147 check_expected_ptr(ptr);
148 check_expected(size);
149 return NULL;
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 bool pcmk__mock_setenv = false;
168
169 int
170 __wrap_setenv(const char *name, const char *value, int overwrite)
171 {
172 if (!pcmk__mock_setenv) {
173 return __real_setenv(name, value, overwrite);
174 }
175 check_expected_ptr(name);
176 check_expected_ptr(value);
177 check_expected(overwrite);
178 errno = mock_type(int);
179 return (errno == 0)? 0 : -1;
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 bool pcmk__mock_unsetenv = false;
196
197 int
198 __wrap_unsetenv(const char *name)
199 {
200 if (!pcmk__mock_unsetenv) {
201 return __real_unsetenv(name);
202 }
203 check_expected_ptr(name);
204 errno = mock_type(int);
205 return (errno == 0)? 0 : -1;
206 }
207
208
209
210
211
212
213
214
215
216
217 bool pcmk__mock_getpid = false;
218
219 pid_t
220 __wrap_getpid(void)
221 {
222 return pcmk__mock_getpid? mock_type(pid_t) : __real_getpid();
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 bool pcmk__mock_grent = false;
237
238
239 static int group_idx = 0;
240
241
242 static const char* grp0_members[] = {
243 "user0", "user1", NULL
244 };
245
246 static const char* grp1_members[] = {
247 "user1", NULL
248 };
249
250 static const char* grp2_members[] = {
251 "user2", "user1", NULL
252 };
253
254
255
256
257
258
259
260
261
262
263 static const int NUM_GROUPS = 3;
264 static struct group groups[] = {
265 {(char*)"grp0", (char*)"", 0, (char**)grp0_members},
266 {(char*)"grp1", (char*)"", 1, (char**)grp1_members},
267 {(char*)"grp2", (char*)"", 2, (char**)grp2_members},
268 };
269
270
271 void
272 __wrap_setgrent(void) {
273 if (pcmk__mock_grent) {
274 group_idx = 0;
275 } else {
276 __real_setgrent();
277 }
278 }
279
280
281
282
283
284 struct group *
285 __wrap_getgrent(void) {
286 if (pcmk__mock_grent) {
287 if (group_idx >= NUM_GROUPS) {
288 return NULL;
289 }
290 return &groups[group_idx++];
291 } else {
292 return __real_getgrent();
293 }
294 }
295
296 void
297 __wrap_endgrent(void) {
298 if (!pcmk__mock_grent) {
299 __real_endgrent();
300 }
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 bool pcmk__mock_fopen = false;
319
320 FILE *
321 __wrap_fopen(const char *pathname, const char *mode)
322 {
323 if (pcmk__mock_fopen) {
324 check_expected_ptr(pathname);
325 check_expected_ptr(mode);
326 errno = mock_type(int);
327
328 if (errno != 0) {
329 return NULL;
330 } else {
331 return __real_fopen(pathname, mode);
332 }
333
334 } else {
335 return __real_fopen(pathname, mode);
336 }
337 }
338
339 #ifdef HAVE_FOPEN64
340 FILE *
341 __wrap_fopen64(const char *pathname, const char *mode)
342 {
343 if (pcmk__mock_fopen) {
344 check_expected_ptr(pathname);
345 check_expected_ptr(mode);
346 errno = mock_type(int);
347
348 if (errno != 0) {
349 return NULL;
350 } else {
351 return __real_fopen64(pathname, mode);
352 }
353
354 } else {
355 return __real_fopen64(pathname, mode);
356 }
357 }
358 #endif
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376 bool pcmk__mock_getpwnam_r = false;
377
378 int
379 __wrap_getpwnam_r(const char *name, struct passwd *pwd, char *buf,
380 size_t buflen, struct passwd **result)
381 {
382 if (pcmk__mock_getpwnam_r) {
383 int retval = mock_type(int);
384
385 check_expected_ptr(name);
386 check_expected_ptr(pwd);
387 check_expected_ptr(buf);
388 check_expected(buflen);
389 check_expected_ptr(result);
390 *result = mock_ptr_type(struct passwd *);
391 return retval;
392
393 } else {
394 return __real_getpwnam_r(name, pwd, buf, buflen, result);
395 }
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413 bool pcmk__mock_readlink = false;
414
415 ssize_t
416 __wrap_readlink(const char *restrict path, char *restrict buf,
417 size_t bufsize)
418 {
419 if (pcmk__mock_readlink) {
420 const char *contents = NULL;
421
422 check_expected_ptr(path);
423 check_expected_ptr(buf);
424 check_expected(bufsize);
425 errno = mock_type(int);
426 contents = mock_ptr_type(const char *);
427
428 if (errno == 0) {
429 strncpy(buf, contents, bufsize - 1);
430 return strlen(contents);
431 }
432 return -1;
433
434 } else {
435 return __real_readlink(path, buf, bufsize);
436 }
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450 bool pcmk__mock_strdup = false;
451
452 char *
453 __wrap_strdup(const char *s)
454 {
455 if (!pcmk__mock_strdup) {
456 return __real_strdup(s);
457 }
458 check_expected_ptr(s);
459 return NULL;
460 }
461
462