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-2025
   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         tmp_act = g_new (struct sigaction, 1);
  89         memcpy (tmp_act, oldact, sizeof (*tmp_act));
  90     }
  91     else
  92         tmp_act = NULL;
  93     if (sigaction_oldact__captured != NULL)
  94         g_ptr_array_add (sigaction_oldact__captured, tmp_act);
  95 
  96     return sigaction__return_value;
  97 }
  98 
  99 static void
 100 sigaction__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 101 {
 102     sigaction_signum__captured = g_ptr_array_new_with_free_func (g_free);
 103     sigaction_act__captured = g_ptr_array_new_with_free_func (g_free);
 104     sigaction_oldact__captured = g_ptr_array_new_with_free_func (g_free);
 105 }
 106 
 107 static void
 108 sigaction__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 109 {
 110     g_ptr_array_free (sigaction_signum__captured, TRUE);
 111     sigaction_signum__captured = NULL;
 112 
 113     g_ptr_array_free (sigaction_act__captured, TRUE);
 114     sigaction_act__captured = NULL;
 115 
 116     g_ptr_array_free (sigaction_oldact__captured, TRUE);
 117     sigaction_oldact__captured = NULL;
 118 }
 119 
 120 /* --------------------------------------------------------------------------------------------- */
 121 
 122 /* @CapturedValue */
 123 static GPtrArray *signal_signum__captured;
 124 /* @CapturedValue */
 125 static GPtrArray *signal_handler__captured;
 126 /* @ThenReturnValue */
 127 static sighandler_t signal__return_value = NULL;
 128 
 129 /* @Mock */
 130 sighandler_t
 131 my_signal (int signum, sighandler_t handler)
     /* [previous][next][first][last][top][bottom][index][help]  */
 132 {
 133     int *tmp_signum;
 134     sighandler_t *tmp_handler;
 135 
 136     // store signum
 137     tmp_signum = g_new (int, 1);
 138     memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
 139     g_ptr_array_add (signal_signum__captured, tmp_signum);
 140 
 141     // store handler
 142     if (handler != SIG_DFL)
 143     {
 144         tmp_handler = g_new (sighandler_t, 1);
 145         memcpy (tmp_handler, handler, sizeof (*tmp_handler));
 146     }
 147     else
 148         tmp_handler = (void *) SIG_DFL;
 149     g_ptr_array_add (signal_handler__captured, tmp_handler);
 150 
 151     return signal__return_value;
 152 }
 153 
 154 static void
 155 signal__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 156 {
 157     signal_signum__captured = g_ptr_array_new_with_free_func (g_free);
 158     signal_handler__captured = g_ptr_array_new_with_free_func (g_free);
 159 }
 160 
 161 static void
 162 signal__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 163 {
 164     g_ptr_array_free (signal_signum__captured, TRUE);
 165     signal_signum__captured = NULL;
 166 
 167     g_ptr_array_free (signal_handler__captured, TRUE);
 168     signal_handler__captured = NULL;
 169 }
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 /* @ThenReturnValue */
 174 static pid_t fork__return_value;
 175 
 176 /* @Mock */
 177 pid_t
 178 my_fork (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 179 {
 180     return fork__return_value;
 181 }
 182 
 183 /* --------------------------------------------------------------------------------------------- */
 184 /* @CapturedValue */
 185 static int my_exit__status__captured;
 186 
 187 /* @Mock */
 188 void
 189 my_exit (int status)
     /* [previous][next][first][last][top][bottom][index][help]  */
 190 {
 191     my_exit__status__captured = status;
 192 }
 193 
 194 /* --------------------------------------------------------------------------------------------- */
 195 
 196 /* @CapturedValue */
 197 static char *execvp__file__captured = NULL;
 198 /* @CapturedValue */
 199 static GPtrArray *execvp__args__captured;
 200 /* @ThenReturnValue */
 201 static int execvp__return_value = 0;
 202 
 203 /* @Mock */
 204 int
 205 my_execvp (const char *file, char *const argv[])
     /* [previous][next][first][last][top][bottom][index][help]  */
 206 {
 207     char **one_arg;
 208     execvp__file__captured = g_strdup (file);
 209 
 210     for (one_arg = (char **) argv; *one_arg != NULL; one_arg++)
 211         g_ptr_array_add (execvp__args__captured, g_strdup (*one_arg));
 212 
 213     return execvp__return_value;
 214 }
 215 
 216 static void
 217 execvp__init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 218 {
 219     execvp__args__captured = g_ptr_array_new_with_free_func (g_free);
 220 }
 221 
 222 static void
 223 execvp__deinit (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 224 {
 225     g_ptr_array_free (execvp__args__captured, TRUE);
 226     execvp__args__captured = NULL;
 227     MC_PTR_FREE (execvp__file__captured);
 228 }
 229 
 230 /* --------------------------------------------------------------------------------------------- */
 231 
 232 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr)                                                       \
 233     {                                                                                              \
 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);                                                      \
 237     }
 238 
 239 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx)                                         \
 240     {                                                                                              \
 241         struct sigaction *_oldact =                                                                \
 242             (struct sigaction *) g_ptr_array_index (sigaction_oldact__captured, oldact_idx);       \
 243         struct sigaction *_act =                                                                   \
 244             (struct sigaction *) g_ptr_array_index (sigaction_act__captured, act_idx);             \
 245         ck_assert_msg (memcmp (_oldact, _act, sizeof (struct sigaction)) == 0,                     \
 246                        "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx,          \
 247                        act_idx);                                                                   \
 248     }
 249 
 250 /* @Verify */
 251 #define VERIFY_SIGACTION_CALLS()                                                                   \
 252     {                                                                                              \
 253         ck_assert_int_eq (sigaction_signum__captured->len, 6);                                     \
 254                                                                                                    \
 255         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 0)), SIGINT);   \
 256         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 1)), SIGQUIT);  \
 257         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 2)), SIGTSTP);  \
 258         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 3)), SIGINT);   \
 259         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 4)), SIGQUIT);  \
 260         ck_assert_int_eq (*((int *) g_ptr_array_index (sigaction_signum__captured, 5)), SIGTSTP);  \
 261                                                                                                    \
 262         VERIFY_SIGACTION__ACT_IGNORED (g_ptr_array_index (sigaction_act__captured, 0));            \
 263         VERIFY_SIGACTION__ACT_IGNORED (g_ptr_array_index (sigaction_act__captured, 1));            \
 264         {                                                                                          \
 265             struct sigaction *_act = g_ptr_array_index (sigaction_act__captured, 2);               \
 266             ck_assert_msg (                                                                        \
 267                 memcmp (_act, &startup_handler, sizeof (struct sigaction)) == 0,                   \
 268                 "The 'act' in third call to sigaction() should be equals to startup_handler");     \
 269         }                                                                                          \
 270                                                                                                    \
 271         VERIFY_SIGACTION__IS_RESTORED (0, 3);                                                      \
 272         VERIFY_SIGACTION__IS_RESTORED (1, 4);                                                      \
 273         VERIFY_SIGACTION__IS_RESTORED (2, 5);                                                      \
 274                                                                                                    \
 275         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 3) == NULL,                  \
 276                        "oldact in fourth call to sigaction() should be NULL");                     \
 277         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 4) == NULL,                  \
 278                        "oldact in fifth call to sigaction() should be NULL");                      \
 279         ck_assert_msg (g_ptr_array_index (sigaction_oldact__captured, 5) == NULL,                  \
 280                        "oldact in sixth call to sigaction() should be NULL");                      \
 281     }
 282 
 283 /* --------------------------------------------------------------------------------------------- */
 284 
 285 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx)                                                     \
 286     {                                                                                              \
 287         sighandler_t *tmp_handler =                                                                \
 288             (sighandler_t *) g_ptr_array_index (signal_handler__captured, _idx);                   \
 289         mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL);                              \
 290     }
 291 
 292 /* @Verify */
 293 #define VERIFY_SIGNAL_CALLS()                                                                      \
 294     {                                                                                              \
 295         ck_assert_int_eq (signal_signum__captured->len, 4);                                        \
 296         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 0)), SIGINT);      \
 297         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 1)), SIGQUIT);     \
 298         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 2)), SIGTSTP);     \
 299         ck_assert_int_eq (*((int *) g_ptr_array_index (signal_signum__captured, 3)), SIGCHLD);     \
 300                                                                                                    \
 301         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0);                                                      \
 302         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1);                                                      \
 303         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2);                                                      \
 304         VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3);                                                      \
 305     }
 306 
 307 /* --------------------------------------------------------------------------------------------- */
 308 
 309 /* @Before */
 310 static void
 311 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 312 {
 313     signal__return_value = NULL;
 314 
 315     sigaction__init ();
 316     signal__init ();
 317     execvp__init ();
 318 }
 319 
 320 /* --------------------------------------------------------------------------------------------- */
 321 
 322 /* @After */
 323 static void
 324 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 325 {
 326     execvp__deinit ();
 327     signal__deinit ();
 328     sigaction__deinit ();
 329 }
 330 
 331 /* --------------------------------------------------------------------------------------------- */

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