root/tests/lib/utilunix__my_system-fork_child.c

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

DEFINITIONS

This source file includes following definitions.
  1. START_TEST
  2. START_TEST
  3. START_TEST
  4. main

   1 /*
   2    lib - tests 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 #define TEST_SUITE_NAME "/lib/utilunix"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "lib/util.h"
  31 
  32 #include "utilunix__my_system-common.c"
  33 
  34 /* --------------------------------------------------------------------------------------------- */
  35 
  36 START_TEST (fork_child)
     /* [previous][next][first][last][top][bottom][index][help]  */
  37 {
  38     int actual_value;
  39     // given
  40     fork__return_value = 0;
  41 
  42     // when
  43     actual_value = my_system (0, "/bin/some-command", "some parameter");
  44 
  45     // then
  46     ck_assert_int_eq (actual_value, 0);
  47 
  48     VERIFY_SIGACTION_CALLS ();
  49     VERIFY_SIGNAL_CALLS ();
  50 
  51     mctest_assert_str_eq (execvp__file__captured, "/bin/some-command");
  52     ck_assert_int_eq (execvp__args__captured->len, 2);
  53 
  54     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "/bin/some-command");
  55     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "some parameter");
  56 
  57     // All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
  58     // situation
  59     ck_assert_int_eq (my_exit__status__captured, 127);
  60 }
  61 END_TEST
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 
  65 START_TEST (fork_child_tokens)
     /* [previous][next][first][last][top][bottom][index][help]  */
  66 {
  67     int actual_value;
  68     // given
  69     fork__return_value = 0;
  70 
  71     // when
  72     actual_value = my_system (0, "vi +", "foo.txt");
  73 
  74     // then
  75     ck_assert_int_eq (actual_value, 0);
  76 
  77     VERIFY_SIGACTION_CALLS ();
  78     VERIFY_SIGNAL_CALLS ();
  79 
  80     mctest_assert_str_eq (execvp__file__captured, "vi");
  81     ck_assert_int_eq (execvp__args__captured->len, 3);
  82 
  83     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "vi");
  84     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "+");
  85     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 2), "foo.txt");
  86 
  87     // All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
  88     // situation
  89     ck_assert_int_eq (my_exit__status__captured, 127);
  90 }
  91 END_TEST
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 
  95 START_TEST (fork_child_tokens2)
     /* [previous][next][first][last][top][bottom][index][help]  */
  96 {
  97     int actual_value;
  98     // given
  99     fork__return_value = 0;
 100 
 101     // when
 102     actual_value = my_system (0, "qwe -a 'aa bb' -b -c cc -d \"dd ee\" -f ff\\ gg", "foo.txt");
 103 
 104     // then
 105     ck_assert_int_eq (actual_value, 0);
 106 
 107     VERIFY_SIGACTION_CALLS ();
 108     VERIFY_SIGNAL_CALLS ();
 109 
 110     mctest_assert_str_eq (execvp__file__captured, "qwe");
 111     ck_assert_int_eq (execvp__args__captured->len, 11);
 112 
 113     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 0), "qwe");
 114     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 1), "-a");
 115     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 2), "'aa bb'");
 116     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 3), "-b");
 117     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 4), "-c");
 118     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 5), "cc");
 119     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 6), "-d");
 120     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 7), "\"dd ee\"");
 121     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 8), "-f");
 122     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 9), "ff\\ gg");
 123     mctest_assert_str_eq (g_ptr_array_index (execvp__args__captured, 10), "foo.txt");
 124 
 125     // All exec* calls is mocked, so call to _exit() function with 127 status code it's a normal
 126     // situation
 127     ck_assert_int_eq (my_exit__status__captured, 127);
 128 }
 129 END_TEST
 130 
 131 /* --------------------------------------------------------------------------------------------- */
 132 
 133 int
 134 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 135 {
 136     TCase *tc_core;
 137 
 138     tc_core = tcase_create ("Core");
 139 
 140     tcase_add_checked_fixture (tc_core, setup, teardown);
 141 
 142     // Add new tests here: ***************
 143     tcase_add_test (tc_core, fork_child);
 144     tcase_add_test (tc_core, fork_child_tokens);
 145     tcase_add_test (tc_core, fork_child_tokens2);
 146     // ***********************************
 147 
 148     return mctest_run_all (tc_core);
 149 }
 150 
 151 /* --------------------------------------------------------------------------------------------- */

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