Merge branch '4524_cleanup'
[midnight-commander.git] / tests / lib / utilunix__my_system-common.c
blobdb462996c43124620a53a36077d28c08433d1a99
1 /*
2 lib - common code for testing lib/utilinux:my_system() function
4 Copyright (C) 2013-2024
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 /* sighandler_t is GNU extension */
32 #ifndef HAVE_SIGHANDLER_T
33 typedef void (*sighandler_t) (int);
34 #endif
36 /* --------------------------------------------------------------------------------------------- */
38 /* @CapturedValue */
39 static sigset_t *sigemptyset_set__captured;
40 /* @ThenReturnValue */
41 static int sigemptyset__return_value = 0;
43 /* @Mock */
44 int
45 sigemptyset (sigset_t * set)
47 sigemptyset_set__captured = set;
48 return sigemptyset__return_value;
51 /* --------------------------------------------------------------------------------------------- */
53 /* @CapturedValue */
54 static GPtrArray *sigaction_signum__captured = NULL;
55 /* @CapturedValue */
56 static GPtrArray *sigaction_act__captured = NULL;
57 /* @CapturedValue */
58 static GPtrArray *sigaction_oldact__captured = NULL;
59 /* @ThenReturnValue */
60 static int sigaction__return_value = 0;
62 /* @Mock */
63 int
64 sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
66 int *tmp_signum;
67 struct sigaction *tmp_act;
69 /* store signum */
70 tmp_signum = g_new (int, 1);
71 memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
72 if (sigaction_signum__captured != NULL)
73 g_ptr_array_add (sigaction_signum__captured, tmp_signum);
75 /* store act */
76 if (act != NULL)
78 tmp_act = g_new (struct sigaction, 1);
79 memcpy (tmp_act, act, sizeof (*tmp_act));
81 else
82 tmp_act = NULL;
83 if (sigaction_act__captured != NULL)
84 g_ptr_array_add (sigaction_act__captured, tmp_act);
86 /* store oldact */
87 if (oldact != NULL)
89 tmp_act = g_new (struct sigaction, 1);
90 memcpy (tmp_act, oldact, sizeof (*tmp_act));
92 else
93 tmp_act = NULL;
94 if (sigaction_oldact__captured != NULL)
95 g_ptr_array_add (sigaction_oldact__captured, tmp_act);
97 return sigaction__return_value;
100 static void
101 sigaction__init (void)
103 sigaction_signum__captured = g_ptr_array_new_with_free_func (g_free);
104 sigaction_act__captured = g_ptr_array_new_with_free_func (g_free);
105 sigaction_oldact__captured = g_ptr_array_new_with_free_func (g_free);
108 static void
109 sigaction__deinit (void)
111 g_ptr_array_free (sigaction_signum__captured, TRUE);
112 sigaction_signum__captured = NULL;
114 g_ptr_array_free (sigaction_act__captured, TRUE);
115 sigaction_act__captured = NULL;
117 g_ptr_array_free (sigaction_oldact__captured, TRUE);
118 sigaction_oldact__captured = NULL;
121 /* --------------------------------------------------------------------------------------------- */
123 /* @CapturedValue */
124 static GPtrArray *signal_signum__captured;
125 /* @CapturedValue */
126 static GPtrArray *signal_handler__captured;
127 /* @ThenReturnValue */
128 static sighandler_t signal__return_value = NULL;
130 /* @Mock */
131 sighandler_t
132 signal (int signum, sighandler_t handler)
134 int *tmp_signum;
135 sighandler_t *tmp_handler;
137 /* store signum */
138 tmp_signum = g_new (int, 1);
139 memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
140 g_ptr_array_add (signal_signum__captured, tmp_signum);
142 /* store handler */
143 if (handler != SIG_DFL)
145 tmp_handler = g_new (sighandler_t, 1);
146 memcpy (tmp_handler, handler, sizeof (*tmp_handler));
148 else
149 tmp_handler = (void *) SIG_DFL;
150 g_ptr_array_add (signal_handler__captured, tmp_handler);
152 return signal__return_value;
155 static void
156 signal__init (void)
158 signal_signum__captured = g_ptr_array_new_with_free_func (g_free);
159 signal_handler__captured = g_ptr_array_new_with_free_func (g_free);
162 static void
163 signal__deinit (void)
165 g_ptr_array_free (signal_signum__captured, TRUE);
166 signal_signum__captured = 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_with_free_func (g_free);
223 static void
224 execvp__deinit (void)
226 g_ptr_array_free (execvp__args__captured, TRUE);
227 execvp__args__captured = NULL;
228 MC_PTR_FREE (execvp__file__captured);
231 /* --------------------------------------------------------------------------------------------- */
233 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
234 struct sigaction *_act = (struct sigaction *) _pntr; \
235 mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
236 ck_assert_int_eq (_act->sa_flags, 0); \
239 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
240 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
241 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
242 ck_assert_msg (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
243 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
246 /* @Verify */
247 #define VERIFY_SIGACTION_CALLS() { \
248 ck_assert_int_eq (sigaction_signum__captured->len, 6); \
250 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
251 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
252 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
253 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
254 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
255 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
257 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
258 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
260 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
261 ck_assert_msg (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
262 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
265 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
266 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
267 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
269 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
270 "oldact in fourth call to sigaction() should be NULL"); \
271 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
272 "oldact in fifth call to sigaction() should be NULL"); \
273 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
274 "oldact in sixth call to sigaction() should be NULL"); \
277 /* --------------------------------------------------------------------------------------------- */
279 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
280 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
281 mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
284 /* @Verify */
285 #define VERIFY_SIGNAL_CALLS() { \
286 ck_assert_int_eq (signal_signum__captured->len, 4); \
287 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
288 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
289 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
290 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
292 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
293 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
294 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
295 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
298 /* --------------------------------------------------------------------------------------------- */
300 /* @Before */
301 static void
302 setup (void)
304 signal__return_value = NULL;
306 sigaction__init ();
307 signal__init ();
308 execvp__init ();
311 /* --------------------------------------------------------------------------------------------- */
313 /* @After */
314 static void
315 teardown (void)
317 execvp__deinit ();
318 signal__deinit ();
319 sigaction__deinit ();
322 /* --------------------------------------------------------------------------------------------- */