summaryrefslogtreecommitdiffstats
path: root/tests/lib/utilunix__my_system-common.c
blob: d24970ed64b13b0c3b0fad84dd5e2f4d9cc61f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
   lib - common code for testing lib/utilinux:my_system() function

   Copyright (C) 2013-2022
   Free Software Foundation, Inc.

   Written by:
   Slava Zanko <slavazanko@gmail.com>, 2013

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <signal.h>
#include <unistd.h>

#include "lib/vfs/vfs.h"

/* sighandler_t is GNU extension */
#ifndef HAVE_SIGHANDLER_T
typedef void (*sighandler_t) (int);
#endif

/* --------------------------------------------------------------------------------------------- */

/* @CapturedValue */
static sigset_t *sigemptyset_set__captured;
/* @ThenReturnValue */
static int sigemptyset__return_value = 0;

/* @Mock */
int
sigemptyset (sigset_t * set)
{
    sigemptyset_set__captured = set;
    return sigemptyset__return_value;
}

/* --------------------------------------------------------------------------------------------- */

/* @CapturedValue */
static GPtrArray *sigaction_signum__captured = NULL;
/* @CapturedValue */
static GPtrArray *sigaction_act__captured = NULL;
/* @CapturedValue */
static GPtrArray *sigaction_oldact__captured = NULL;
/* @ThenReturnValue */
static int sigaction__return_value = 0;

/* @Mock */
int
sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
{
    int *tmp_signum;
    struct sigaction *tmp_act;

    /* store signum */
    tmp_signum = g_new (int, 1);
    memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
    if (sigaction_signum__captured != NULL)
        g_ptr_array_add (sigaction_signum__captured, tmp_signum);

    /* store act */
    if (act != NULL)
    {
        tmp_act = g_new (struct sigaction, 1);
        memcpy (tmp_act, act, sizeof (*tmp_act));
    }
    else
        tmp_act = NULL;
    if (sigaction_act__captured != NULL)
        g_ptr_array_add (sigaction_act__captured, tmp_act);

    /* store oldact */
    if (oldact != NULL)
    {
        tmp_act = g_new (struct sigaction, 1);
        memcpy (tmp_act, oldact, sizeof (*tmp_act));
    }
    else
        tmp_act = NULL;
    if (sigaction_oldact__captured != NULL)
        g_ptr_array_add (sigaction_oldact__captured, tmp_act);

    return sigaction__return_value;
}

static void
sigaction__init (void)
{
    sigaction_signum__captured = g_ptr_array_new ();
    sigaction_act__captured = g_ptr_array_new ();
    sigaction_oldact__captured = g_ptr_array_new ();
}

static void
sigaction__deinit (void)
{
    g_ptr_array_foreach (sigaction_signum__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (sigaction_signum__captured, TRUE);
    sigaction_signum__captured = NULL;

    g_ptr_array_foreach (sigaction_act__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (sigaction_act__captured, TRUE);
    sigaction_act__captured = NULL;

    g_ptr_array_foreach (sigaction_oldact__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (sigaction_oldact__captured, TRUE);
    sigaction_oldact__captured = NULL;
}

/* --------------------------------------------------------------------------------------------- */

/* @CapturedValue */
static GPtrArray *signal_signum__captured;
/* @CapturedValue */
static GPtrArray *signal_handler__captured;
/* @ThenReturnValue */
static sighandler_t signal__return_value = NULL;

/* @Mock */
sighandler_t
signal (int signum, sighandler_t handler)
{
    int *tmp_signum;
    sighandler_t *tmp_handler;

    /* store signum */
    tmp_signum = g_new (int, 1);
    memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
    g_ptr_array_add (signal_signum__captured, tmp_signum);

    /* store handler */
    if (handler != SIG_DFL)
    {
        tmp_handler = g_new (sighandler_t, 1);
        memcpy (tmp_handler, handler, sizeof (*tmp_handler));
    }
    else
        tmp_handler = (void *) SIG_DFL;
    g_ptr_array_add (signal_handler__captured, tmp_handler);

    return signal__return_value;
}

static void
signal__init (void)
{
    signal_signum__captured = g_ptr_array_new ();
    signal_handler__captured = g_ptr_array_new ();
}

static void
signal__deinit (void)
{
    g_ptr_array_foreach (signal_signum__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (signal_signum__captured, TRUE);
    signal_signum__captured = NULL;

    g_ptr_array_foreach (signal_handler__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (signal_handler__captured, TRUE);
    signal_handler__captured = NULL;
}

/* --------------------------------------------------------------------------------------------- */

/* @ThenReturnValue */
static pid_t fork__return_value;

/* @Mock */
pid_t
fork (void)
{
    return fork__return_value;
}

/* --------------------------------------------------------------------------------------------- */
/* @CapturedValue */
static int my_exit__status__captured;

/* @Mock */
void
my_exit (int status)
{
    my_exit__status__captured = status;
}

/* --------------------------------------------------------------------------------------------- */

/* @CapturedValue */
static char *execvp__file__captured = NULL;
/* @CapturedValue */
static GPtrArray *execvp__args__captured;
/* @ThenReturnValue */
static int execvp__return_value = 0;

/* @Mock */
int
execvp (const char *file, char *const argv[])
{
    char **one_arg;
    execvp__file__captured = g_strdup (file);

    for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
        g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));

    return execvp__return_value;
}

static void
execvp__init (void)
{
    execvp__args__captured = g_ptr_array_new ();
}

static void
execvp__deinit (void)
{
    g_ptr_array_foreach (execvp__args__captured, (GFunc) g_free, NULL);
    g_ptr_array_free (execvp__args__captured, TRUE);
    execvp__args__captured = NULL;
    MC_PTR_FREE (execvp__file__captured);
}

/* --------------------------------------------------------------------------------------------- */

#define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
    struct sigaction *_act = (struct sigaction *) _pntr; \
    mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
    ck_assert_int_eq (_act->sa_flags, 0); \
}

#define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
    struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
    struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
    ck_assert_msg (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
        "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
}

/* @Verify */
#define VERIFY_SIGACTION_CALLS() { \
    ck_assert_int_eq (sigaction_signum__captured->len, 6); \
\
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
\
    VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
    VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
    { \
        struct sigaction *_act  = g_ptr_array_index(sigaction_act__captured, 2); \
        ck_assert_msg (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
            "The 'act' in third call to sigaction() should be equals to startup_handler"); \
    } \
\
    VERIFY_SIGACTION__IS_RESTORED (0, 3); \
    VERIFY_SIGACTION__IS_RESTORED (1, 4); \
    VERIFY_SIGACTION__IS_RESTORED (2, 5); \
\
    ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
        "oldact in fourth call to sigaction() should be NULL"); \
    ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
        "oldact in fifth call to sigaction() should be NULL"); \
    ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
        "oldact in sixth call to sigaction() should be NULL"); \
}

/* --------------------------------------------------------------------------------------------- */

#define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
    sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
    mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
}

/* @Verify */
#define VERIFY_SIGNAL_CALLS() { \
    ck_assert_int_eq (signal_signum__captured->len, 4); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
    ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
    \
    VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
    VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
    VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
    VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
}

/* --------------------------------------------------------------------------------------------- */

/* @Before */
static void
setup (void)
{
    signal__return_value = NULL;

    sigaction__init ();
    signal__init ();
    execvp__init ();
}

/* --------------------------------------------------------------------------------------------- */

/* @After */
static void
teardown (void)
{
    execvp__deinit ();
    signal__deinit ();
    sigaction__deinit ();
}

/* --------------------------------------------------------------------------------------------- */