Add new functions:
[midnight-commander.git] / tests / lib / utilunix__my_system-common.c
blob5741591b59c13d98d96b3f109c7f313056339029
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 *execvp__file__captured = NULL;
195 /* @CapturedValue */
196 static GPtrArray *execvp__args__captured;
197 /* @ThenReturnValue */
198 static int execvp__return_value = 0;
200 /* @Mock */
202 execvp (const char *file, char *const argv[])
204 char **one_arg;
205 execvp__file__captured = g_strdup (file);
207 for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
208 g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
210 return execvp__return_value;
213 static void
214 execvp__init (void)
216 execvp__args__captured = g_ptr_array_new ();
219 static void
220 execvp__deinit (void)
222 g_ptr_array_foreach (execvp__args__captured, (GFunc) g_free, NULL);
223 g_ptr_array_free (execvp__args__captured, TRUE);
224 g_free (execvp__file__captured);
227 /* --------------------------------------------------------------------------------------------- */
229 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
230 struct sigaction *_act = (struct sigaction *) _pntr; \
231 fail_unless (_act->sa_handler == SIG_IGN, " sa_handler should be equals to SIG_IGN"); \
232 ck_assert_int_eq (_act->sa_flags, 0); \
235 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
236 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
237 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
238 fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
239 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
242 /* @Verify */
243 #define VERIFY_SIGACTION_CALLS() { \
244 ck_assert_int_eq (sigaction_signum__captured->len, 6); \
246 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
247 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
248 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
249 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
250 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
251 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
253 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
254 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
256 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
257 fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
258 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
261 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
262 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
263 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
265 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
266 "oldact in fourth call to sigaction() should be NULL"); \
267 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
268 "oldact in fifth call to sigaction() should be NULL"); \
269 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
270 "oldact in sixth call to sigaction() should be NULL"); \
273 /* --------------------------------------------------------------------------------------------- */
275 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
276 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
277 fail_unless (tmp_handler == (sighandler_t *) SIG_DFL, \
278 "signal handler should be SIG_DFL"); \
281 /* @Verify */
282 #define VERIFY_SIGNAL_CALLS() { \
283 ck_assert_int_eq (signal_signum__captured->len, 4); \
284 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
285 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
286 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
287 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
289 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
290 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
291 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
292 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
295 /* --------------------------------------------------------------------------------------------- */
297 /* @Before */
298 static void
299 setup (void)
301 signal__return_value = NULL;
303 sigaction__init ();
304 signal__init ();
305 execvp__init ();
308 /* --------------------------------------------------------------------------------------------- */
310 /* @After */
311 static void
312 teardown (void)
314 execvp__deinit ();
315 signal__deinit ();
316 sigaction__deinit ();
319 /* --------------------------------------------------------------------------------------------- */