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

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