Add tests for lib/utilunix.c:my_system() function
[midnight-commander.git] / tests / lib / utilunix__my_system-common.c
blob69354cef0094dce7dd8d42ddfa29434c38f97294
1 /*
2 lib - common code for testing lib/utilinux:my_system() function
4 Copyright (C) 2013
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <signal.h>
27 #include <unistd.h>
29 #include "lib/global.h"
32 /* --------------------------------------------------------------------------------------------- */
34 /* @CapturedValue */
35 static sigset_t *sigemptyset_set__captured;
36 /* @ThenReturnValue */
37 static int sigemptyset__return_value = 0;
39 /* @Mock */
40 int
41 sigemptyset (sigset_t * set)
43 sigemptyset_set__captured = set;
44 return sigemptyset__return_value;
47 /* --------------------------------------------------------------------------------------------- */
49 /* @CapturedValue */
50 static GPtrArray *sigaction_signum__captured = NULL;
51 /* @CapturedValue */
52 static GPtrArray *sigaction_act__captured = NULL;
53 /* @CapturedValue */
54 static GPtrArray *sigaction_oldact__captured = NULL;
55 /* @ThenReturnValue */
56 static int sigaction__return_value = 0;
58 /* @Mock */
59 int
60 sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
62 int *tmp_signum;
63 struct sigaction *tmp_act;
65 /* store signum */
66 tmp_signum = g_new (int, 1);
67 memcpy (tmp_signum, &signum, sizeof (int));
68 if (sigaction_signum__captured != NULL)
69 g_ptr_array_add (sigaction_signum__captured, tmp_signum);
71 /* store act */
72 if (act != NULL)
74 tmp_act = g_new (struct sigaction, 1);
75 memcpy (tmp_act, act, sizeof (struct sigaction));
77 else
78 tmp_act = NULL;
79 if (sigaction_act__captured != NULL)
80 g_ptr_array_add (sigaction_act__captured, tmp_act);
82 /* store oldact */
83 if (oldact != NULL)
85 tmp_act = g_new (struct sigaction, 1);
86 memcpy (tmp_act, oldact, sizeof (struct sigaction));
88 else
89 tmp_act = NULL;
90 if (sigaction_oldact__captured != NULL)
91 g_ptr_array_add (sigaction_oldact__captured, tmp_act);
93 return sigaction__return_value;
96 static void
97 sigaction__init (void)
99 sigaction_signum__captured = g_ptr_array_new ();
100 sigaction_act__captured = g_ptr_array_new ();
101 sigaction_oldact__captured = g_ptr_array_new ();
104 static void
105 sigaction__deinit (void)
107 g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL);
108 g_ptr_array_free (sigaction_signum__captured, TRUE);
110 g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL);
111 g_ptr_array_free (sigaction_act__captured, TRUE);
113 g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL);
114 g_ptr_array_free (sigaction_oldact__captured, TRUE);
117 /* --------------------------------------------------------------------------------------------- */
119 /* @CapturedValue */
120 static GPtrArray *signal_signum__captured;
121 /* @CapturedValue */
122 static GPtrArray *signal_handler__captured;
123 /* @ThenReturnValue */
124 static sighandler_t signal__return_value = NULL;
126 /* @Mock */
127 sighandler_t
128 signal (int signum, sighandler_t handler)
130 int *tmp_signum;
131 sighandler_t *tmp_handler;
133 /* store signum */
134 tmp_signum = g_new (int, 1);
135 memcpy (tmp_signum, &signum, sizeof (int));
136 g_ptr_array_add (signal_signum__captured, tmp_signum);
138 /* store handler */
139 if (handler != SIG_DFL)
141 tmp_handler = g_new (sighandler_t, 1);
142 memcpy (tmp_handler, handler, sizeof (sighandler_t));
144 else
145 tmp_handler = (void *) SIG_DFL;
146 g_ptr_array_add (signal_handler__captured, tmp_handler);
148 return signal__return_value;
151 static void
152 signal__init (void)
154 signal_signum__captured = g_ptr_array_new ();
155 signal_handler__captured = g_ptr_array_new ();
158 static void
159 signal__deinit (void)
161 g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL);
162 g_ptr_array_free (signal_signum__captured, TRUE);
164 g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL);
165 g_ptr_array_free (signal_handler__captured, TRUE);
168 /* --------------------------------------------------------------------------------------------- */
170 /* @ThenReturnValue */
171 static pid_t fork__return_value;
173 /* @Mock */
174 pid_t
175 fork (void)
177 return fork__return_value;
180 /* --------------------------------------------------------------------------------------------- */
181 /* @CapturedValue */
182 static int my_exit__status__captured;
184 /* @Mock */
185 void
186 my_exit (int status)
188 my_exit__status__captured = status;
191 /* --------------------------------------------------------------------------------------------- */
193 /* @CapturedValue */
194 static char *execl__path__captured = NULL;
195 /* @CapturedValue */
196 static char *execl__arg__captured = NULL;
197 /* @CapturedValue */
198 static GPtrArray *execl__args__captured;
199 /* @ThenReturnValue */
200 static int execl__return_value = 0;
202 /* @Mock */
204 execl (const char *path, const char *arg, ...)
206 char *one_arg;
207 va_list vargs;
209 execl__path__captured = g_strdup (path);
210 execl__arg__captured = g_strdup (arg);
212 va_start (vargs, arg);
214 while ((one_arg = va_arg (vargs, char *)) != NULL)
216 g_ptr_array_add (execl__args__captured, g_strdup (one_arg));
218 va_end (vargs);
220 return execl__return_value;
223 static void
224 execl__init (void)
226 execl__args__captured = g_ptr_array_new ();
229 static void
230 execl__deinit (void)
232 g_ptr_array_foreach (execl__args__captured, (GFunc) g_free, NULL);
233 g_ptr_array_free (execl__args__captured, TRUE);
234 g_free (execl__path__captured);
235 g_free (execl__arg__captured);
238 /* --------------------------------------------------------------------------------------------- */
240 /* @CapturedValue */
241 static char *execlp__file__captured = NULL;
242 /* @CapturedValue */
243 static char *execlp__arg__captured = NULL;
244 /* @CapturedValue */
245 static GPtrArray *execlp__args__captured;
246 /* @ThenReturnValue */
247 static int execlp__return_value = 0;
249 /* @Mock */
251 execlp(const char *file, const char *arg, ...)
253 char *one_arg;
254 va_list vargs;
256 execlp__file__captured = g_strdup (file);
257 execlp__arg__captured = g_strdup (arg);
259 va_start (vargs, arg);
261 while ((one_arg = va_arg (vargs, char *)) != NULL)
263 g_ptr_array_add (execlp__args__captured, g_strdup (one_arg));
265 va_end (vargs);
267 return execlp__return_value;
270 static void
271 execlp__init (void)
273 execlp__args__captured = g_ptr_array_new ();
276 static void
277 execlp__deinit (void)
279 g_ptr_array_foreach (execlp__args__captured, (GFunc) g_free, NULL);
280 g_ptr_array_free (execlp__args__captured, TRUE);
282 g_free (execlp__file__captured);
283 g_free (execlp__arg__captured);
286 /* --------------------------------------------------------------------------------------------- */
288 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
289 struct sigaction *_act = (struct sigaction *) _pntr; \
290 fail_unless (_act->sa_handler == SIG_IGN, " sa_handler should be equals to SIG_IGN"); \
291 ck_assert_int_eq (_act->sa_flags, 0); \
294 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
295 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
296 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
297 fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
298 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
301 /* @Verify */
302 #define VERIFY_SIGACTION_CALLS() { \
303 ck_assert_int_eq (sigaction_signum__captured->len, 6); \
305 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
306 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
307 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
308 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
309 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
310 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
312 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
313 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
315 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
316 fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
317 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
320 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
321 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
322 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
324 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
325 "oldact in fourth call to sigaction() should be NULL"); \
326 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
327 "oldact in fifth call to sigaction() should be NULL"); \
328 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
329 "oldact in sixth call to sigaction() should be NULL"); \
332 /* --------------------------------------------------------------------------------------------- */
334 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
335 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
336 fail_unless (tmp_handler == (sighandler_t *) SIG_DFL, \
337 "signal handler should be SIG_DFL"); \
340 /* @Verify */
341 #define VERIFY_SIGNAL_CALLS() { \
342 ck_assert_int_eq (signal_signum__captured->len, 4); \
343 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
344 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
345 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
346 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
348 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
349 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
350 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
351 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
354 /* --------------------------------------------------------------------------------------------- */
356 /* @Before */
357 static void
358 setup (void)
360 signal__return_value = NULL;
362 sigaction__init ();
363 signal__init ();
364 execl__init ();
365 execlp__init ();
368 /* --------------------------------------------------------------------------------------------- */
370 /* @After */
371 static void
372 teardown (void)
374 execlp__deinit ();
375 execl__deinit ();
376 signal__deinit ();
377 sigaction__deinit ();
380 /* --------------------------------------------------------------------------------------------- */