(mc_search_run): document the return value.
[midnight-commander.git] / tests / lib / utilunix__my_system-common.c
blob92a587a1bc618b6e6627d877ecdf73867ffc1832
1 /*
2 lib - common code for testing lib/utilinux:my_system() function
4 Copyright (C) 2013-2016
5 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/vfs/vfs.h"
31 /* --------------------------------------------------------------------------------------------- */
33 /* @CapturedValue */
34 static sigset_t *sigemptyset_set__captured;
35 /* @ThenReturnValue */
36 static int sigemptyset__return_value = 0;
38 /* @Mock */
39 int
40 sigemptyset (sigset_t * set)
42 sigemptyset_set__captured = set;
43 return sigemptyset__return_value;
46 /* --------------------------------------------------------------------------------------------- */
48 /* @CapturedValue */
49 static GPtrArray *sigaction_signum__captured = NULL;
50 /* @CapturedValue */
51 static GPtrArray *sigaction_act__captured = NULL;
52 /* @CapturedValue */
53 static GPtrArray *sigaction_oldact__captured = NULL;
54 /* @ThenReturnValue */
55 static int sigaction__return_value = 0;
57 /* @Mock */
58 int
59 sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
61 int *tmp_signum;
62 struct sigaction *tmp_act;
64 /* store signum */
65 tmp_signum = g_new (int, 1);
66 memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
67 if (sigaction_signum__captured != NULL)
68 g_ptr_array_add (sigaction_signum__captured, tmp_signum);
70 /* store act */
71 if (act != NULL)
73 tmp_act = g_new (struct sigaction, 1);
74 memcpy (tmp_act, act, sizeof (*tmp_act));
76 else
77 tmp_act = NULL;
78 if (sigaction_act__captured != NULL)
79 g_ptr_array_add (sigaction_act__captured, tmp_act);
81 /* store oldact */
82 if (oldact != NULL)
84 tmp_act = g_new (struct sigaction, 1);
85 memcpy (tmp_act, oldact, sizeof (*tmp_act));
87 else
88 tmp_act = NULL;
89 if (sigaction_oldact__captured != NULL)
90 g_ptr_array_add (sigaction_oldact__captured, tmp_act);
92 return sigaction__return_value;
95 static void
96 sigaction__init (void)
98 sigaction_signum__captured = g_ptr_array_new ();
99 sigaction_act__captured = g_ptr_array_new ();
100 sigaction_oldact__captured = g_ptr_array_new ();
103 static void
104 sigaction__deinit (void)
106 g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL);
107 g_ptr_array_free (sigaction_signum__captured, TRUE);
108 sigaction_signum__captured = NULL;
110 g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL);
111 g_ptr_array_free (sigaction_act__captured, TRUE);
112 sigaction_act__captured = NULL;
114 g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL);
115 g_ptr_array_free (sigaction_oldact__captured, TRUE);
116 sigaction_oldact__captured = NULL;
119 /* --------------------------------------------------------------------------------------------- */
121 /* @CapturedValue */
122 static GPtrArray *signal_signum__captured;
123 /* @CapturedValue */
124 static GPtrArray *signal_handler__captured;
125 /* @ThenReturnValue */
126 static sighandler_t signal__return_value = NULL;
128 /* @Mock */
129 sighandler_t
130 signal (int signum, sighandler_t handler)
132 int *tmp_signum;
133 sighandler_t *tmp_handler;
135 /* store signum */
136 tmp_signum = g_new (int, 1);
137 memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
138 g_ptr_array_add (signal_signum__captured, tmp_signum);
140 /* store handler */
141 if (handler != SIG_DFL)
143 tmp_handler = g_new (sighandler_t, 1);
144 memcpy (tmp_handler, handler, sizeof (*tmp_handler));
146 else
147 tmp_handler = (void *) SIG_DFL;
148 g_ptr_array_add (signal_handler__captured, tmp_handler);
150 return signal__return_value;
153 static void
154 signal__init (void)
156 signal_signum__captured = g_ptr_array_new ();
157 signal_handler__captured = g_ptr_array_new ();
160 static void
161 signal__deinit (void)
163 g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL);
164 g_ptr_array_free (signal_signum__captured, TRUE);
165 signal_signum__captured = NULL;
167 g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL);
168 g_ptr_array_free (signal_handler__captured, TRUE);
169 signal_handler__captured = NULL;
172 /* --------------------------------------------------------------------------------------------- */
174 /* @ThenReturnValue */
175 static pid_t fork__return_value;
177 /* @Mock */
178 pid_t
179 fork (void)
181 return fork__return_value;
184 /* --------------------------------------------------------------------------------------------- */
185 /* @CapturedValue */
186 static int my_exit__status__captured;
188 /* @Mock */
189 void
190 my_exit (int status)
192 my_exit__status__captured = status;
195 /* --------------------------------------------------------------------------------------------- */
197 /* @CapturedValue */
198 static char *execvp__file__captured = NULL;
199 /* @CapturedValue */
200 static GPtrArray *execvp__args__captured;
201 /* @ThenReturnValue */
202 static int execvp__return_value = 0;
204 /* @Mock */
206 execvp (const char *file, char *const argv[])
208 char **one_arg;
209 execvp__file__captured = g_strdup (file);
211 for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
212 g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
214 return execvp__return_value;
217 static void
218 execvp__init (void)
220 execvp__args__captured = g_ptr_array_new ();
223 static void
224 execvp__deinit (void)
226 g_ptr_array_foreach (execvp__args__captured, (GFunc) g_free, NULL);
227 g_ptr_array_free (execvp__args__captured, TRUE);
228 execvp__args__captured = NULL;
229 MC_PTR_FREE (execvp__file__captured);
232 /* --------------------------------------------------------------------------------------------- */
234 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
235 struct sigaction *_act = (struct sigaction *) _pntr; \
236 mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
237 mctest_assert_int_eq (_act->sa_flags, 0); \
240 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
241 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
242 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
243 fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
244 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
247 /* @Verify */
248 #define VERIFY_SIGACTION_CALLS() { \
249 mctest_assert_int_eq (sigaction_signum__captured->len, 6); \
251 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
252 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
253 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
254 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
255 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
256 mctest_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
258 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
259 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
261 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
262 fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
263 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
266 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
267 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
268 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
270 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
271 "oldact in fourth call to sigaction() should be NULL"); \
272 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
273 "oldact in fifth call to sigaction() should be NULL"); \
274 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
275 "oldact in sixth call to sigaction() should be NULL"); \
278 /* --------------------------------------------------------------------------------------------- */
280 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
281 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
282 mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
285 /* @Verify */
286 #define VERIFY_SIGNAL_CALLS() { \
287 mctest_assert_int_eq (signal_signum__captured->len, 4); \
288 mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
289 mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
290 mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
291 mctest_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
293 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
294 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
295 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
296 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
299 /* --------------------------------------------------------------------------------------------- */
301 /* @Before */
302 static void
303 setup (void)
305 signal__return_value = NULL;
307 sigaction__init ();
308 signal__init ();
309 execvp__init ();
312 /* --------------------------------------------------------------------------------------------- */
314 /* @After */
315 static void
316 teardown (void)
318 execvp__deinit ();
319 signal__deinit ();
320 sigaction__deinit ();
323 /* --------------------------------------------------------------------------------------------- */