Manual pages: mc • mcdiff • mcedit • mcview

root/tests/lib/utilunix__my_system-common.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. sigemptyset
  2. my_sigaction
  3. sigaction__init
  4. sigaction__deinit
  5. my_signal
  6. signal__init
  7. signal__deinit
  8. my_fork
  9. my_exit
  10. my_execvp
  11. execvp__init
  12. execvp__deinit
  13. setup
  14. teardown

   1 /*
   2    lib - common code for testing lib/utilinux:my_system() function
   3 
   4    Copyright (C) 2013-2026
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  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.
  16 
  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.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  24  */
  25 
  26 #include <signal.h>
  27 #include <unistd.h>
  28 
  29 #include "lib/vfs/vfs.h"
  30 
  31 /* --------------------------------------------------------------------------------------------- */
  32 
  33 /* @CapturedValue */
  34 static sigset_t *sigemptyset_set__captured;
  35 /* @ThenReturnValue */
  36 static int sigemptyset__return_value = 0;
  37 
  38 #ifdef sigemptyset
  39 #undef sigemptyset
  40 #endif
  41 
  42 /* @Mock */
  43 int
  44 sigemptyset (sigset_t *set)
     /* [previous][next][first][last][top][bottom][index][help]  */
  45 {
  46     sigemptyset_set__captured = set;
  47     return sigemptyset__return_value;
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 /* @CapturedValue */
  53 static GPtrArray *sigaction_signum__captured = NULL;
  54 /* @CapturedValue */
  55 static GPtrArray *sigaction_act__captured = NULL;
  56 /* @CapturedValue */
  57 static GPtrArray *sigaction_oldact__captured = NULL;
  58 /* @ThenReturnValue */
  59 static int sigaction__return_value = 0;
  60 
  61 /* @Mock */
  62 int
  63 my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
     /* [previous][next][first][last][top][bottom][index][help]  */
  64 {
  65     int *tmp_signum;
  66     struct sigaction *tmp_act;
  67 
  68     // store signum
  69     tmp_signum = g_new (int, 1);
  70     memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
  71     if (sigaction_signum__captured != NULL)
  72         g_ptr_array_add (sigaction_signum__captured, tmp_signum);
  73 
  74     // store act
  75     if (act != NULL)
  76     {
  77         tmp_act = g_new (struct sigaction, 1);
  78         memcpy (tmp_act, act, sizeof (*tmp_act));
  79     }
  80     else
  81         tmp_act = NULL;
  82     if (sigaction_act__captured != NULL)
  83         g_ptr_array_add (sigaction_act__captured, tmp_act);
  84 
  85     // store oldact
  86     if (oldact != NULL)
  87     {
  88         /* The real sigaction() fills this in; without the same here the test
  89            compares bytes that nobody has ever written. */
  90         memset (oldact, 0, sizeof (*oldact));
  91         tmp_act = g_new (struct sigaction, 1);
  92         memcpy (tmp_act, oldact, sizeof (*tmp_act));
  93     }
  94     else
  95         tmp_act = NULL;
  96     if (sigaction_oldact__captured != NULL)
  97         g_ptr_array_add (sigaction_oldact__captured, tmp_act);
  98 
  99     return sigaction__return_value;
 100 }
 101 
 102 static void
 103 sigaction__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 104 {
 105     sigaction_signum__captured = g_ptr_array_new_with_free_func (g_free);
 106     sigaction_act__captured = g_ptr_array_new_with_free_func (g_free);
 107     sigaction_oldact__captured = g_ptr_array_new_with_free_func (g_free);
 108 }
 109 
 110 static void
 111 sigaction__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 112 {
 113     g_ptr_array_free (sigaction_signum__captured, TRUE);
 114     sigaction_signum__captured = NULL;
 115 
 116     g_ptr_array_free (sigaction_act__captured, TRUE);
 117     sigaction_act__captured = NULL;
 118 
 119     g_ptr_array_free (sigaction_oldact__captured, TRUE);
 120     sigaction_oldact__captured = NULL;
 121 }
 122 
 123 /* --------------------------------------------------------------------------------------------- */
 124 
 125 /* @CapturedValue */
 126 static GPtrArray *signal_signum__captured;
 127 /* @CapturedValue */
 128 static GPtrArray *signal_handler__captured;
 129 /* @ThenReturnValue */
 130 static sighandler_t signal__return_value = NULL;
 131 
 132 /* @Mock */
 133 sighandler_t
 134 my_signal (int signum, sighandler_t handler)
     /* [previous][next][first][last][top][bottom][index][help]  */
 135 {
 136     int *tmp_signum;
 137     sighandler_t *tmp_handler;
 138 
 139     // store signum
 140     tmp_signum = g_new (int, 1);
 141     memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
 142     g_ptr_array_add (signal_signum__captured, tmp_signum);
 143 
 144     // store handler
 145     if (handler != SIG_DFL)
 146     {
 147         tmp_handler = g_new (sighandler_t, 1);
 148         memcpy (tmp_handler, handler, sizeof (*tmp_handler));
 149     }
 150     else
 151         tmp_handler = (void *) SIG_DFL;
 152     g_ptr_array_add (signal_handler__captured, tmp_handler);
 153 
 154     return signal__return_value;
 155 }
 156 
 157 static void
 158 signal__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 159 {
 160     signal_signum__captured = g_ptr_array_new_with_free_func (g_free);
 161     signal_handler__captured = g_ptr_array_new_with_free_func (g_free);
 162 }
 163 
 164 static void
 165 signal__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 166 {
 167     g_ptr_array_free (signal_signum__captured, TRUE);
 168     signal_signum__captured = NULL;
 169 
 170     g_ptr_array_free (signal_handler__captured, TRUE);
 171     signal_handler__captured = NULL;
 172 }
 173 
 174 /* --------------------------------------------------------------------------------------------- */
 175 
 176 /* @ThenReturnValue */
 177 static pid_t fork__return_value;
 178 
 179 /* @Mock */
 180 pid_t
 181 my_fork (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 182 {
 183     return fork__return_value;
 184 }
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 /* @CapturedValue */
 188 static int my_exit__status__captured;
 189 
 190 /* @Mock */
 191 void
 192 my_exit (int status)
     /* [previous][next][first][last][top][bottom][index][help]  */
 193 {
 194     my_exit__status__captured = status;
 195 }
 196 
 197 /* --------------------------------------------------------------------------------------------- */
 198 
 199 /* @CapturedValue */
 200 static char *execvp__file__captured = NULL;
 201 /* @CapturedValue */
 202 static GPtrArray *execvp__args__captured;
 203 /* @ThenReturnValue */
 204 static int execvp__return_value = 0;
 205 
 206 /* @Mock */
 207 int
 208 my_execvp (const char *file, char *const argv[])
     /* [previous][next][first][last][top][bottom][index][help]  */
 209 {
 210     char **one_arg;
 211     execvp__file__captured = g_strdup (file);
 212 
 213     for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
 214         g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
 215 
 216     return execvp__return_value;
 217 }
 218 
 219 static void
 220 execvp__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 221 {
 222     execvp__args__captured = g_ptr_array_new_with_free_func (g_free);
 223 }
 224 
 225 static void
 226 execvp__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     g_ptr_array_free (execvp__args__captured, TRUE);
 229     execvp__args__captured = NULL;
 230     MC_PTR_FREE (execvp__file__captured);
 231 }
 232 
 233 /* --------------------------------------------------------------------------------------------- */
 234 
 235 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr)                                                       \
 236     {                                                                                              \
 237         struct sigaction *_act = (struct sigaction *) _pntr;                                       \
 238         mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN);                                          \
 239         ck_assert_int_eq (_act->sa_flags, 0);                                                      \
 240     }
 241 
 242 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx)                                         \
 243     {                                                                                              \
 244         struct sigaction *_oldact =                                                                \
 245             (struct sigaction *) g_ptr_array_index (sigaction_oldact__captured, oldact_idx);       \
 246         struct sigaction *_act =                                                                   \
 247             (struct sigaction *) g_ptr_array_index (sigaction_act__captured, act_idx);             \
 248         ck_assert_msg (memcmp (_oldact, _act, sizeof (struct sigaction)) == 0,                     \
 249                        "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx,          \
 250                        act_idx);                                                                   \
 251     }
 252 
 253 /* @Verify */
 254 #define VERIFY_SIGACTION_CALLS()                                                                   \
 255     {                                                                                              \
 256         ck_assert_int_eq (sigaction_signum__captured->len, 6);                                     \
 257                                                                                                    \
 258         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 0)), SIGINT);   \
 259         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 1)), SIGQUIT);  \
 260         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 2)), SIGTSTP);  \
 261         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 3)), SIGINT);   \
 262         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 4)), SIGQUIT);  \
 263         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 5)), SIGTSTP);  \
 264                                                                                                    \
 265         VERIFY_SIGACTION__ACT_IGNORED (g_ptr_array_index (sigaction_act__captured, 0));            \
 266         VERIFY_SIGACTION__ACT_IGNORED (g_ptr_array_index (sigaction_act__captured, 1));            \
 267         {                                                                                          \
 268             struct sigaction *_act = g_ptr_array_index (sigaction_act__captured, 2);               \
 269             ck_assert_msg (                                                                        \
 270                 memcmp (_act, &startup_handler, sizeof (struct sigaction)) == 0,                   \
 271                 "The 'act' in third call to sigaction() should be equals to startup_handler");     \
 272         }                                                                                          \
 273                                                                                                    \
 274         VERIFY_SIGACTION__IS_RESTORED (0, 3);                                                      \
 275         VERIFY_SIGACTION__IS_RESTORED (1, 4);                                                      \
 276         VERIFY_SIGACTION__IS_RESTORED (2, 5);                                                      \
 277                                                                                                    \
 278         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 3) == NULL,                  \
 279                        "oldact in fourth call to sigaction() should be NULL");                     \
 280         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 4) == NULL,                  \
 281                        "oldact in fifth call to sigaction() should be NULL");                      \
 282         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 5) == NULL,                  \
 283                        "oldact in sixth call to sigaction() should be NULL");                      \
 284     }
 285 
 286 /* --------------------------------------------------------------------------------------------- */
 287 
 288 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx)                                                     \
 289     {                                                                                              \
 290         sighandler_t *tmp_handler =                                                                \
 291             (sighandler_t *) g_ptr_array_index (signal_handler__captured, _idx);                   \
 292         mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL);                              \
 293     }
 294 
 295 /* @Verify */
 296 #define VERIFY_SIGNAL_CALLS()                                                                      \
 297     {                                                                                              \
 298         ck_assert_int_eq (signal_signum__captured->len, 4);                                        \
 299         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 0)), SIGINT);      \
 300         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 1)), SIGQUIT);     \
 301         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 2)), SIGTSTP);     \
 302         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 3)), SIGCHLD);     \
 303                                                                                                    \
 304         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0);                                                      \
 305         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1);                                                      \
 306         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2);                                                      \
 307         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3);                                                      \
 308     }
 309 
 310 /* --------------------------------------------------------------------------------------------- */
 311 
 312 /* @Before */
 313 static void
 314 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 315 {
 316     signal__return_value = NULL;
 317 
 318     sigaction__init ();
 319     signal__init ();
 320     execvp__init ();
 321 }
 322 
 323 /* --------------------------------------------------------------------------------------------- */
 324 
 325 /* @After */
 326 static void
 327 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 328 {
 329     execvp__deinit ();
 330     signal__deinit ();
 331     sigaction__deinit ();
 332 }
 333 
 334 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */