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