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